package com.bluishoul.action;

import java.io.IOException;

import my.mvc.Annotation.JSONOutputEnabled;
import my.mvc.RequestContext;

import org.apache.commons.lang.StringUtils;

import com.bluishoul.bean.Dish;

public class DishAction {
	@JSONOutputEnabled
	public void update(RequestContext rc) throws IOException{
		Dish dish = rc.form(Dish.class);
		_checkDishInfo(rc,dish);
		if(dish.getId()==0L)
			dish.Save();
		else{
			if(dish.Get(dish.getId())!=null)
				dish.Update();
			else
				throw rc.error("dish_not_exist");
		}
		rc.output_json("id", dish.getId());
	}
	
	private void _checkDishInfo(RequestContext rc,Dish dish){
		if(dish==null)
			throw rc.error("dish_not_null");
		Dish ex = dish.hasSame();
		if(ex!=null && dish.getId()==0L)
			throw rc.error("dish_exist", ex.getId(),ex.getName());
		if(StringUtils.isBlank(dish.getName()))
			throw rc.error("dish_name_not_empty");
		if(dish.getPrice()==0f)
			throw rc.error("dish_price_not_zero");
	}
}
